package com.xoa.service.financialManage.Impl;

import com.xoa.dao.financialManage.IncomeExpensePlanMapper;
import com.xoa.dao.financialManage.IncomeExpenseRecordsMapper;
import com.xoa.model.financialManage.IncomeExpensePlanWithBLOBs;
import com.xoa.model.financialManage.IncomeExpenseRecordsWithBLOBs;
import com.xoa.service.financialManage.IncomeExpenseRecordsService;
import com.xoa.util.DateFormat;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@Service
public class IncomeExpenseRecordsServiceImpl implements IncomeExpenseRecordsService {

    @Autowired
    private IncomeExpenseRecordsMapper recordsMapper;

    @Autowired
    private IncomeExpensePlanMapper planMapper;

    //查询
    @Override
    public ToJson findRecords(HttpServletRequest request, IncomeExpenseRecordsWithBLOBs recordsWithBLOBs) {
        ToJson toJson=new ToJson(1,"暂无数据");
        try {
            String sqlType = "xoa" + (String) request.getSession().getAttribute("loginDateSouse");

            Map map=new HashMap();
            //类型查询
            map.put("planId",recordsWithBLOBs.getPlanId());

            //时间查询
            map.put("createdReTime",recordsWithBLOBs.getCreatedReTime());
            map.put("endReTime",recordsWithBLOBs.getEndReTime());

            List<IncomeExpenseRecordsWithBLOBs> RecordsWithBLOBs = recordsMapper.selectRecords(map);
            for (IncomeExpenseRecordsWithBLOBs records:RecordsWithBLOBs) {
                //附件信息
                if (!StringUtils.checkNull(records.getAttachmentName()) && !StringUtils.checkNull(records.getAttachmentId())) {
                    records.setAttachmentList(GetAttachmentListUtil.returnAttachment(records.getAttachmentName(), records.getAttachmentId(), sqlType, "revenue"));
                }
            }
            if (RecordsWithBLOBs.size()>0){
                toJson.setFlag(0);
                toJson.setMsg("ok");
                toJson.setObj(RecordsWithBLOBs);
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    //根据ID查询
    @Override
    public ToJson findRecordsById(HttpServletRequest request, IncomeExpenseRecordsWithBLOBs recordsWithBLOBs) {
        ToJson toJson=new ToJson(1,"暂无数据");
        try {
            String sqlType = "xoa" + request.getSession().getAttribute("loginDateSouse");

            IncomeExpenseRecordsWithBLOBs incomeExpenseRecordsWithBLOBs = recordsMapper.selectByPrimaryKey(recordsWithBLOBs.getRecordId());
            if (incomeExpenseRecordsWithBLOBs!=null){
                if (!StringUtils.checkNull(incomeExpenseRecordsWithBLOBs.getAttachmentName()) && !StringUtils.checkNull(incomeExpenseRecordsWithBLOBs.getAttachmentId())) {
                    incomeExpenseRecordsWithBLOBs.setAttachmentList(GetAttachmentListUtil.returnAttachment(incomeExpenseRecordsWithBLOBs.getAttachmentName(), incomeExpenseRecordsWithBLOBs.getAttachmentId(), sqlType, "revenue"));
                }

                toJson.setMsg("ok");
                toJson.setFlag(0);
                toJson.setObject(incomeExpenseRecordsWithBLOBs);
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    //修改和新增
    @Override
    public ToJson editRecords(HttpServletRequest request, IncomeExpenseRecordsWithBLOBs recordsWithBLOBs) {
        ToJson toJson=new ToJson(1,"err");
        try {
            int i=0;
            if (recordsWithBLOBs.getRecordId()!=null){
                i = recordsMapper.updateByPrimaryKeySelective(recordsWithBLOBs);
            }else{
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);//获取年份
                int month=cal.get(Calendar.MONTH)+1;//获取月份
                int day=cal.get(Calendar.DATE);//获取日
                int season=0;  //获取季节
                if(month>=1 && month<=3){
                    season=1; //春
                }else if (month>=4 && month<=6){
                    season=2; //夏
                }else if (month>=7 && month<=9){
                    season=3; //秋
                }else if (month>=10){
                    season=4; //冬
                }
                recordsWithBLOBs.setRecordYear(String.valueOf(year));
                recordsWithBLOBs.setRecordMonth(String.valueOf(month));
                recordsWithBLOBs.setRecordDay(String.valueOf(day));
                recordsWithBLOBs.setRecordQuarter(String.valueOf(season));
                //创建时间
                recordsWithBLOBs.setCreatedReTime(new Date());
                i = recordsMapper.insertSelective(recordsWithBLOBs);
            }
            if (i>0){
                //因为要统计总收入和总支出，每次都会进行查询进行统计
                Integer planId = recordsWithBLOBs.getPlanId();
                this.sumExpenseOrIncome(planId);

                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    //删除
    @Override
    public ToJson deleteRecords(HttpServletRequest request, IncomeExpenseRecordsWithBLOBs recordsWithBLOBs) {
        ToJson toJson=new ToJson(1,"err");
        try {
            int i = recordsMapper.deleteByPrimaryKey(recordsWithBLOBs.getRecordId());
            if (i>0){
                this.sumExpenseOrIncome(recordsWithBLOBs.getPlanId());
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    //图表
    @Override
    public ToJson findstatistics(HttpServletRequest request, IncomeExpenseRecordsWithBLOBs recordsWithBLOBs) {
        ToJson toJson=new ToJson(1,"err");
        try {
            List<Map<String,Object>> objectMap=recordsMapper.findstatistics(recordsWithBLOBs.getPlanId());
            for (Map<String,Object> objectMap1:objectMap) {
                Date record_time = (Date) objectMap1.get("RECORD_TIME");
                objectMap1.put("RECORD_TIME",DateFormat.getStrDate(record_time));
            }
            if (objectMap.size()>0){
                toJson.setObject(objectMap);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e){
            e.getMessage();
        }
        return toJson;
    }


    //修改计划总金额
    private void sumExpenseOrIncome(Integer planId){
        if (planId!=null){
            Map map=new HashMap();
            map.put("planId",planId);
            Map<String,Object> countmap=planMapper.countExpenseOrIncome(map);
            IncomeExpensePlanWithBLOBs planWithBLOBs=new IncomeExpensePlanWithBLOBs();
            planWithBLOBs.setRealExpense((BigDecimal)countmap.get("SUMEXPENSE")); //总支出
            planWithBLOBs.setRealIncome((BigDecimal)countmap.get("SUMINCOME")); //总收入
            planWithBLOBs.setPlanId(planId); //主键
            planMapper.updateByPrimaryKeySelective(planWithBLOBs);
        }
    }
}
